package pe.gob.sedapal.scmultimedia.util;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.logging.Logger;

import com.mchange.v2.codegen.bean.InnerBeanPropertyBeanGenerator;

public class CodigoIpUtil {
	private int numero;
	private String ip;
	private InetAddress address;
	
	public CodigoIpUtil(){

		
	}
	/*
	public static void main(String args[]){
		try{
			InetAddress ruta = InetAddress.getLocalHost();
			String numeroIp = ruta.getHostName();
			byte[] bIPAddress = ruta.getAddress();
			System.out.println("RUTA = "+ruta);
			System.out.println("IP = "+numeroIp);
			
			// IP en formato String
			String sIPAddress = "";
			for (int x=0; x<bIPAddress.length; x++) {
				if (x > 0) {
				// A todos los numeros les anteponemos
				// un punto menos al primero
				sIPAddress += ".";
				}
				// Jugamos con los bytes y cambiamos el bit del signo
				sIPAddress += bIPAddress[x] & 255;
			}
			System.out.println("HOST = "+sIPAddress);
			
			String localIpAddress = InetAddress.getLocalHost().getHostAddress();
			System.out.println("localIpAdress = "+localIpAddress);
			
			InetAddress lh = ruta.getLocalHost();
			System.out.println(lh.getCanonicalHostName());
			
			
			try {
		        String hostName = InetAddress.getLocalHost().getHostName();
		        System.out.println("HostName = " + hostName);
		        System.out.println("HostAddressLocal = " +
		          InetAddress.getLocalHost().getHostAddress());
		        InetAddress[] inetAddresses = InetAddress.getAllByName(hostName);
		        for (InetAddress inetAddress : inetAddresses) {
		          System.out.println("hostAddress = " + inetAddress.getHostAddress());
		        }
		      } catch (Exception e) {
		          e.printStackTrace();
		      }

			
				System.out.println("=======================================");
				
				try {
				      System.out.println( "-> Direccion IP de una URL, por nombre" );  
				      InetAddress address = InetAddress.getByName( "nereida.deioc.ull.es" );
				      System.out.println( address );

				      // Extrae la dirección IP a partir de la cadena que se
				      // encuentra a la derecha de la barra /, luego proporciona
				      // esta dirección IP como argumento de llamada al método getByName()

				      System.out.println( "-> Nombre a partir de la direccion" );
				      int temp = address.toString().indexOf( '/' );
				      address = InetAddress.getByName( address.toString().substring(temp+1) );
				      System.out.println( address );
				       
				      System.out.println( "-> Direccion IP actual de LocalHost" );
				      address = InetAddress.getLocalHost();
				      System.out.println( address );

				      System.out.println( "-> Nombre de LocalHost a partir de la direccion" );
				      temp = address.toString().indexOf( '/' );
				      address = InetAddress.getByName( address.toString().substring(temp+1) );
				      System.out.println( address );

				      System.out.println( "-> Nombre actual de LocalHost" );
				      System.out.println( address.getHostName() );
				       
				      System.out.println( "-> Direccion IP actual de LocalHost" );
				      // Coge la dirección IP como un array de bytes
				      byte[] bytes = address.getAddress();
				      // Convierte los bytes de la dirección IP a valores sin
				      // signo y los presenta separados por espacios
				      for( int cnt=0; cnt < bytes.length; cnt++ ) {
				        int uByte = bytes[cnt] < 0 ? bytes[cnt]+256 : bytes[cnt];
				        System.out.print( uByte+" " );
				      }
				      System.out.println();
				    } 
				    catch( UnknownHostException e ) {
				      System.out.println( e );
				      System.out.println( "Debes estar conectado para que esto funcione bien." );
				    }				
				
				System.out.println("***********************************************");
				InetAddress ruta2 = Inet6Address.getLocalHost();
				String host = ruta2.getHostName();
				System.out.println("RUTA = "+host);
				
				LinkedList<InetAddress> listAdr = new LinkedList<InetAddress>();
		        try
		        {
		            Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
		            if (nifs == null) System.out.println("NULL");// return listAdr;
		 
		            while (nifs.hasMoreElements())
		            {
		                NetworkInterface nif = nifs.nextElement();
		                // We ignore subinterfaces - as not yet needed.
		 
		                Enumeration<InetAddress> adrs = nif.getInetAddresses();
		                while (adrs.hasMoreElements())
		                {
		                    InetAddress adr = adrs.nextElement();
		                    if (adr != null && !adr.isLoopbackAddress() && (nif.isPointToPoint() || !adr.isLinkLocalAddress()))
		                    {
		                        listAdr.add(adr);
		                        System.out.println("ADR = "+adr);
		                    }
		                }
		            }
		            System.out.println("LIST = "+listAdr);//return listAdr;
		        }
		        catch (SocketException ex)
		        {
		        	System.out.println("EXCEPT");
		            //Logger.getLogger(Net.class.getName()).log(Level.WARNING, "No IP address available", ex);
		            System.out.println("LIST = "+listAdr);//return listAdr;
		        }				
				
		}catch(Exception e){
			System.out.println("NO SE PUDO");
		}		
		
	}
	*/
	public int getNumero() {
		return numero;
	}
	public void setNumero(int numero) {
		this.numero = numero;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}
	
	
}
